﻿/*
    Parker for Kaixin001.com 

    Copyright (C) 2008, TurboZV, turbozv@gmail.com, http://www.turbozv.com/

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

// by xzsong.seu@gmail.com, 2008-09-20
using System;
using System.Collections.Generic;
using Helper;
using System.Collections.ObjectModel;

namespace Parker
{
    [Serializable]
    class NewCarInfo:IComparable<NewCarInfo>
    {
        private int _carId;
        private string _carName;
        private int _carPrice;

        public bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(_carName) && (_carPrice > 0);
            }
        }

        public int CarId
        {
            get
            {
                return _carId;
            }
            set
            {
                _carId = value;
            }
        }
        public string CarName
        {
            get
            {
                return _carName;
            }
            set
            {
                _carName = value;
            }
        }
        public int CarPrice
        {
            get
            {
                return _carPrice;
            }
            set
            {
                _carPrice = value;
            }
        }

        public int CompareTo(NewCarInfo other)
        {
            return _carPrice - other.CarPrice;
        }
    }


    [Serializable]
    class CarPoolManager
    {
        private List<NewCarInfo> _carList = new List<NewCarInfo>();
        private Dictionary<string, NewCarInfo> _carMapping = new Dictionary<string, NewCarInfo>();

        public string GetCarName(int carId)
        {
            foreach (KeyValuePair<string, NewCarInfo> carInfoPair in _carMapping)
            {
                if (carInfoPair.Value.CarId == carId) return carInfoPair.Value.CarName;
            }
            return "<未知车辆>";
        }

        public string GetExistingCarName(int carId, Dictionary<string, int> existingCars)
        {
            foreach (KeyValuePair<string, int> carInfoPair in existingCars)
            {
                if (carInfoPair.Value == carId) return carInfoPair.Key;
            }
            return "<未知车辆>";
        }

        public void AddNewCar(NewCarInfo newCarInfo)
        {
            if (!_carMapping.ContainsKey(newCarInfo.CarName))
            {
                _carMapping.Add(newCarInfo.CarName, newCarInfo);
                _carList.Add(newCarInfo);
            }
        }

        public void Save()
        {
            _carList.Sort();
            SerializeHelper.Serialize<CarPoolManager>(this);
        }

        public List<int> GetBuyCarIdList(Collection<int> NoBuyCarIdList,Dictionary<string, int> existingCars, int requestNumber, ref int cash)
        {
            List<int> toBuyIdList = new List<int>();
            foreach (NewCarInfo newCarInfo in _carList)
            {
                if (!NoBuyCarIdList.Contains(newCarInfo.CarId) && !existingCars.ContainsKey(newCarInfo.CarName))
                {
                    if (newCarInfo.CarPrice < cash)
                    {
                        toBuyIdList.Add(newCarInfo.CarId);
                        cash -= newCarInfo.CarPrice;
                        requestNumber--;
                        if (requestNumber <= 0)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return toBuyIdList;
        }

        public List<KeyValuePair<int, int>> GetUpdateCarIdPairList(Collection<int> NoUpgradeCarIdList, Dictionary<string, int> existingCars, ref int cash, bool bBestFirst)
        {
            List<KeyValuePair<int, int>> updateKeyValuePairList = new List<KeyValuePair<int, int>>();

            List<NewCarInfo> existingCarInfos = new List<NewCarInfo>();
            foreach (string carName in existingCars.Keys)
            {
                if (_carMapping.ContainsKey(carName))
                {
                    existingCarInfos.Add(_carMapping[carName]);
                }
            }
            existingCarInfos.Sort();

            if (bBestFirst)
            {
                int i = existingCarInfos.Count - 1;
                int j = _carList.Count - 1;
                while (existingCarInfos[i].CarPrice >= _carList[j].CarPrice && j >= 0 && i >= 0)
                {
                    i--;
                    j--;
                }
                if (i >= 0)
                {
                    NewCarInfo oldCar = existingCarInfos[i];
                    while (j >= 0)
                    {
                        NewCarInfo newCar = _carList[j--];
                        int dis=newCar.CarPrice-oldCar.CarPrice;
                        if (dis > 0)
                        {
                            if (dis < cash)
                            {
                                updateKeyValuePairList.Add(new KeyValuePair<int, int>(newCar.CarId, existingCars[oldCar.CarName]));
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }               
            }
            else
            {
                NewCarInfo oldCar = existingCarInfos[0];
                for (int i = _carList.Count - 1; i >= 0; i--)
                {
                    NewCarInfo newCar = _carList[i];
                    if (!NoUpgradeCarIdList.Contains(newCar.CarId) && !existingCars.ContainsKey(newCar.CarName))
                    {
                        int dis = newCar.CarPrice - oldCar.CarPrice;
                        if (dis > 0)
                        {
                            if (dis < cash)
                            {
                                updateKeyValuePairList.Add(new KeyValuePair<int, int>(newCar.CarId, existingCars[oldCar.CarName]));
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            
            return updateKeyValuePairList;
        }
    }
}
    